﻿using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_ExamPlace_SpecialityKinds_List : BaseForm
    {
        ServiceBase service = new ServiceBase();
        public Base_ExamPlace_SpecialityKinds_List()
        {
            InitializeComponent();
            BindPlaceList();
            BindInfo();
            BindGridView();
        }

        #region 绑定考点
        void BindPlaceList()
        {
            DataTable dt = service.GetSysExamPlaceDataTable("","","1");
            listExamPlace.DisplayMember = "ExamPlaceName";
            listExamPlace.ValueMember = "ExamPlaceNO";
            listExamPlace.DataSource = dt;
        }
        #endregion

        #region 绑定专业
        private void BindInfo()
        {
            using (DataTable dt = service.GetSpecialityKindsTable("", "", "1", ""))
            {
                listSpeciality.DisplayMember = "SpecialityKindsName";
                listSpeciality.ValueMember = "SpecialityKindsNO";
                listSpeciality.DataSource = dt;
            }
        }
        #endregion

        DataTable GetGridViewDataSource()
        {
            string ExamPlaceID = string.Empty;
            string SpecialityKindsNO = string.Empty;
            if (listExamPlace.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                ExamPlaceID = drExamPlace.Row.ItemArray[0].ToSafeString();
            }
            if (listSpeciality.SelectedItem != null)
            {
                DataRowView drExamLevel = (DataRowView)listSpeciality.SelectedItem;
                SpecialityKindsNO = drExamLevel.Row.ItemArray[0].ToSafeString();
            }        
            DataTable dt = service.GetExamPlaceSpecialityKindsMappingTable(ExamPlaceID, SpecialityKindsNO);
         
            return dt;
        }

        void BindGridView()
        {
            //if (listExamPlace.SelectedIndex < 0)
            //{
            //    return;
            //}
            //if (listSpeciality.SelectedIndex < 0)
            //{
            //    return;
            //}
            dataGridViewX1.DataSource = GetGridViewDataSource();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //if (listExamPlace.SelectedIndex < 0)
            //{
            //    DevHelper.ShowInfoMessageBox("请先选择考点!");
            //    return;
            //}
            //if (listSpeciality.SelectedIndex < 0)
            //{
            //    DevHelper.ShowInfoMessageBox("请先选择专业!");
            //    return;
            //}
            string ExamPlaceID = string.Empty;
            string SpecialityKindsNO = string.Empty;
            if (listExamPlace.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                ExamPlaceID = drExamPlace.Row.ItemArray[0].ToSafeString();
            }
            if (listSpeciality.SelectedItem != null)
            {
                DataRowView drExamLevel = (DataRowView)listSpeciality.SelectedItem;
                SpecialityKindsNO = drExamLevel.Row.ItemArray[0].ToSafeString();
            }

            using (DataTable dt = service.GetSpecialityLevelExamTypeMappingNameTable(SpecialityKindsNO, "", ""))
            {
                if (dt == null || dt.Rows.Count == 0)
                {
                    DevHelper.ShowInfoMessageBox("请先进行专业信息考试级别与考试方式设置!");
                    return;
                }
            }
            using (DataTable dt = GetGridViewDataSource())
            {
                //if (dt != null && dt.Rows.Count > 0)
                //{
                //    DevHelper.ShowInfoMessageBox("该考点和专业已经设置!");
                //    return;
                //}
            }
            Base_ExamPlace_SpecialityKinds_Option option = new Base_ExamPlace_SpecialityKinds_Option(string.Empty, listExamPlace.SelectedValue.ToSafeString(), listSpeciality.SelectedValue.ToSafeString());
            if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindGridView();
            }
        }

        private void listExamPlace_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        private void listSpeciality_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedValue() != null)
            {
                string strExamPlaceSpecialityKindsMappingID = string.Empty;
                string strExamPlaceNO = string.Empty;
                string strSpecialityKindsNO = string.Empty;
                GridView rowsSelected = gridView1 as GridView;
                if (rowsSelected.Rows.Count > 0)
                {
                    int[] inteurm = rowsSelected.GetSelectedRows();
                    if (inteurm != null && inteurm.Length > 0)
                    {
                        strExamPlaceSpecialityKindsMappingID = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                        strExamPlaceNO = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                        strSpecialityKindsNO = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[2].ToString();
                        Base_ExamPlace_SpecialityKinds_Option option = new Base_ExamPlace_SpecialityKinds_Option(strExamPlaceSpecialityKindsMappingID, strExamPlaceNO, strSpecialityKindsNO);
                        if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            BindGridView();
                        }

                    }
                }
            }
        }
        private string GetSpecialityKindsNo()
        {
            string strSpecialityKindsNo_Detail = string.Empty;
            GridView rowsSelected = gridView1 as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo_Detail = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                }
            }
            return strSpecialityKindsNo_Detail;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            string strSpecialityKindsNo_Detail = GetSpecialityKindsNo();
            if (strSpecialityKindsNo_Detail.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择删除项!");
            }
            else
            {
                service.DeleteExamPlaceSpecialityKindsMapping(strSpecialityKindsNo_Detail);
                BindGridView();
            }
        }
    }
}
